<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 提示框 -->
      <div class="login_remind">
        <i class="el-icon-edit">请输入你的信息</i>
        <hr />
      </div>
      <!-- 表单 -->
      <el-form class="login_form" ref="loginFormRef" :model="loginForm" :rules="loginFormRules">
        <!-- 用户名 -->
        <el-form-item class="login_form_item" prop="username">
          <el-input v-model="loginForm.username" placeholder="用户名" prefix-icon="fas fa-user"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item class="login_form_item" prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="密码"
            prefix-icon="fas fa-lock"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="login_form_item">
          <el-button @click="login" class="login_button" type="primary">登录</el-button>
        </el-form-item>
      </el-form>
      <!-- 跳转到注册 -->
      <div class="to_register">
        <p>
          还没有账号？
          <a href="/register">免费注册</a>
        </p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 登陆表单
      loginForm: {
        username: "",
        password: ""
      },
      // 登陆表单验证
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 4, max: 10, message: "长度在 4 到 10 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 16, message: "长度在 6 到 16 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    onSubmit() {
      // console.log(this);
      console.log(this.loginForm.username);
      console.log(this.loginForm.password);
      this.login();
    },
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "/user/login",
          this.loginForm
        );
        console.log(res);
        if (res.code == 200) {
          this.$notify({
          title: "登陆成功",
          message: res.message,
          type: "success"
        });

          //将user信息保存到sessionStorage
          sessionStorage.clear();
          sessionStorage.setItem("user", this.$qs.stringify(res.data.user));

          this.$router.push({ path: "/product" });

          if(res.data.user.role == 0){
            this.$router.push({ path: "/admin" });
          }else{
            this.$router.push({ path: "/product" });
          }

        } else {

          this.$message.error(res.message);
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
// 登陆框背景颜色
.login_container {
}
// 登陆框
.login_box {
  background-color: #f5f7fa;
  width: 350px;
  height: 400px;
  position: absolute;
  left: 60%;
  top: 20%;
  border-radius: 20px;
}
// 登陆提示
.login_remind {
  width: 80%;
  margin: auto;
  margin-top: 50px;
  i {
    font-size: 18px;
    font-weight: 700;
    line-height: 0.5;
  }
}
// 登陆表单
.login_form {
  width: 80%;
  margin: auto;
  position: relative;
  top: 10px;

  // 登陆按钮
  .login_button {
    width: 100%;
  }
  // 每个item
  .login_form_item {
    padding-top: 10px;
  }
}
.to_register {
  padding-top: 10px;
  text-align: center;
  a {
    text-decoration: none;
  }
}
</style>
